package com.realtyManage.springboot.Config;

import org.apache.http.HttpHeaders;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter{

    /**
     * 杭州素色网络科技有限公司
     * 2019.08.08
     * 过滤器配置
     */
	private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addExposedHeader(HttpHeaders.AUTHORIZATION);
        //corsConfiguration.addExposedHeader(HttpHeaders.ACCEPT);
        //corsConfiguration.addExposedHeader(HttpHeaders.CONTENT_TYPE);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
    
    /*
    * @Author Danny
    * @Description //配置拦截器 WebMvcConfigurerAdapter已过时 不推荐使用
     *
    * @Date 2019/5/6
    * @Param [registry]
    * @Return void
    **/
    @Override  
    public void addCorsMappings(CorsRegistry registry) {  
        registry.addMapping("/**")  
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowCredentials(true)//允许跨域  
                .allowedMethods("GET", "POST", "DELETE", "PUT")  
                .maxAge(3600);
    }  
}
